package com.SweetWhite.UserManagement.model.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.SweetWhite.UserManagement.model.bean.Hero_User;
import com.SweetWhite.UserManagement.model.service.UpdateService;



@Controller
public class NewPassController extends HttpServlet{
/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
//	@Autowired
//	private SelectService s_service;
	@Autowired
	private UpdateService u_service;

@RequestMapping("newPass")
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	// TODO Auto-generated method stub
	String np=req.getParameter("newPass");
	String cf=req.getParameter("confirm");
	PrintWriter pw=resp.getWriter();
	resp.setContentType("text/html;charset=UTF-8");
	try {
		if (np.equals(cf)) {
			HttpSession session =req.getSession();
			Hero_User hu=(Hero_User) session.getAttribute("user");
			hu.setPassword(np);
			u_service.updateHero_User(hu);
			pw.print("<script>alert('修改成功');window.open('login.jsp')</script>");
		}else {
			pw.print("<script>alert('两次输入密码不一致');history.back()</script>");
		}	
	} catch (Exception e) {
		// TODO: handle exception
		pw.print("<script>alert('密码不能为空');history.back()</script>");
	}
}
}